package com.aistar.service.redis_collect.Impl;

import com.aistar.config.C_LikedStatusEnum;
import com.aistar.pojo.TravCollect;
import com.aistar.pojo.Travelnote;
//import com.aistar.service.redis_collect_liked.LikedCollectService;
import com.aistar.service.redis_collect.RedisCollectService;
import com.aistar.util.RedisCollectKeyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author shkstart
 * @date 2019/10/16 - 2:22
 */
@Service("RedisCollectServiceImpl")
public class RedisCollectServiceImpl implements RedisCollectService {
    @Autowired
    RedisTemplate redisTemplate;


    @Override
    public void saveLiked2Redis(String likedUserId, String likedPostId) {
        String key = RedisCollectKeyUtils.getLikedKey(likedUserId, likedPostId);
        redisTemplate.opsForHash().put(RedisCollectKeyUtils.MAP_KEY_USER_LIKED, key, C_LikedStatusEnum.LIKE.getCode());
    }

    @Override
    public void unlikeFromRedis(String likedUserId, String likedPostId) {
        String key = RedisCollectKeyUtils.getLikedKey(likedUserId, likedPostId);
        redisTemplate.opsForHash().put(RedisCollectKeyUtils.MAP_KEY_USER_LIKED, key, C_LikedStatusEnum.UNLIKE.getCode());
    }

    @Override
    public void deleteLikedFromRedis(String likedUserId, String likedPostId) {
        String key = RedisCollectKeyUtils.getLikedKey(likedUserId, likedPostId);
        redisTemplate.opsForHash().delete(RedisCollectKeyUtils.MAP_KEY_USER_LIKED, key);
    }

    @Override
    public void incrementLikedCount(String likedUserId) {
        redisTemplate.opsForHash().increment(RedisCollectKeyUtils.MAP_KEY_USER_LIKED_COUNT, likedUserId, 1);
    }

    @Override
    public void decrementLikedCount(String likedUserId) {
        redisTemplate.opsForHash().increment(RedisCollectKeyUtils.MAP_KEY_USER_LIKED_COUNT, likedUserId, -1);
    }

    @Override
    public List<TravCollect> getLikedDataFromRedis() {
        Cursor<Map.Entry<Object, Object>> cursor = redisTemplate.opsForHash().scan(RedisCollectKeyUtils.MAP_KEY_USER_LIKED, ScanOptions.NONE);
        List<TravCollect> list = new ArrayList<>();
        while (cursor.hasNext()){
            Map.Entry<Object, Object> entry = cursor.next();
            String key = (String) entry.getKey();
            //分离出 likedUserId，likedPostId
            String[] split = key.split("::");
            int likedUserId = Integer.parseInt(split[0]);
            System.out.println("split[0]:"+split[0]);
            int likedPostId = Integer.parseInt(split[1]);
            System.out.println("split[1]:"+split[1]);
            int value = (Integer) entry.getValue();

            //组装成 UserLike 对象
            TravCollect userLike = new TravCollect(likedUserId, likedPostId, value);
            list.add(userLike);

            //存到 list 后从 Redis 中删除
            redisTemplate.opsForHash().delete(RedisCollectKeyUtils.MAP_KEY_USER_LIKED, key);
        }

        return list;
    }

//    @Override
//    public List<Travelnote> getLikedCountFromRedis() {
//        System.out.println("开始执行。。。。");
//        Cursor<Map.Entry<Object, Object>> cursor = redisTemplate.opsForHash().scan(RedisCollectKeyUtils.MAP_KEY_USER_LIKED, ScanOptions.NONE);
//        System.out.println("cursor:"+cursor.toString());
//        List<Travelnote> list = new ArrayList<>();
//        while (cursor.hasNext()){
//            Map.Entry<Object, Object> map = cursor.next();
//            //将点赞数量存储在 LikedCountDT
//            String key = (String)map.getKey();
//            System.out.println("key:"+key);
//            String id[] = key.split("::");
//            System.out.println("id[1]:"+id[1]);
//            Travelnote dto = new Travelnote(Integer.parseInt(id[1]),  Integer.parseInt(String.valueOf(map.getValue())));
//            System.out.println("dto:"+dto);
//            list.add(dto);
//            //从Redis中删除这条记录
//            redisTemplate.opsForHash().delete(RedisCollectKeyUtils.MAP_KEY_USER_LIKED_COUNT, key);
//        }
//        System.out.println("Redis数据量："+list.size());
//        return list;
//    }
}
